//
//  OrderManageConViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/9/27.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "OrderManageConViewController.h"
#import "OrderManageTableViewCell.h"

@interface OrderManageConViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mytableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *mydateSource;
@property (nonatomic, strong) UIView *tabFootView;

@property (nonatomic, assign) BOOL rightChose;
@property (nonatomic, assign) BOOL secChose;
@property (nonatomic, assign) BOOL rowChose;
@property (nonatomic, assign) BOOL allChose;

@property (nonatomic, strong) UIButton *secSelBtn; //分区区头选择按钮
@property (nonatomic, strong) UIView *bgViewsecCancle; //取消原因 弹窗
@property (nonatomic, strong) UIView *bgViewsecConfirm; //修改数量 弹窗

@end

@implementation OrderManageConViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

//- (void)viewDidDisappear:(BOOL)animated {
//    [super viewDidDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:NO];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"预订管理";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    
    //[self setupRightItem];
    [self requestData];
    [self showdetaile];
    [self shwoBgviewsecCancle]; //取消原因
    [self shwoBgviewsecConfirm]; //修改数量
    // Do any additional setup after loading the view.
}

//预订管理 数据请求
- (void)requestData {
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    NSInteger ind = [_type integerValue];
    
    ind-=1;
    if (ind >1) {
        ind +=1;
    }
    NSString *stt  = [NSString stringWithFormat:@"%ld",ind];
    if (ind == -1) {
        stt = @"";
    }
    
    // 订单状态(0生成订单（未处理）//添加候保配件
    // ，1已处理，//查看配件详情
    param[@"state"] = stt;
    [weakSelf.mydateSource removeAllObjects];
    [NetWorkTool POST:partsOrders param:param success:^(id dic) {
        for (NSDictionary *moDic in [dic objectForKey:@"data"]) {
            [DYModelMaker DY_makeModelWithDictionary:moDic modelKeyword:@"" modelName:@""];
//            CSmaintenanceListModel *model = [[CSmaintenanceListModel alloc]init];
//            [model setValuesForKeysWithDictionary:moDic];
//            [weakSelf.mydateSource addObject:model];
        }
        [self.mytableView reloadData];
        [self.mytableView.mj_header endRefreshing];
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [weakSelf.mydateSource removeAllObjects];
        [self.mytableView reloadData];
        [self.mytableView.mj_header endRefreshing];
        
    } fail:^(NSError *error) {
        [weakSelf.mydateSource removeAllObjects];
        [self.mytableView reloadData];
        [self.mytableView.mj_header endRefreshing];
        
    } needUser:YES];
}

-(void)showdetaile {
    
//    UIView *bgview = [[UIView alloc]initWithFrame:CGRectMake(0,kNaviHeight, KWIDTH, 120)];
//    [self.scrollView addSubview:bgview];
    [self.view addSubview:self.scrollView];
//    bgview.backgroundColor = [UIColor whiteColor];
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 56)];
    
    _mytableView = [[UITableView alloc] init];
    _mytableView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT-kNaviHeight-kTabbarHeight-56);
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    _mytableView.estimatedRowHeight = 90;
    _mytableView.rowHeight = UITableViewAutomaticDimension;
    _mytableView.tableFooterView = [UIView new];
    [_mytableView registerClass:[OrderManageTableViewCell class] forCellReuseIdentifier:@"OrderManageTableViewCell"];
    _mytableView.backgroundColor = [UIColor clearColor];
    adjustInset(_mytableView);
    [_mytableView reloadData];
    _mytableView.tableFooterView = self.tabFootView;
    [_scrollView addSubview:_mytableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //return _mydateSource.count;
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 116;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    headerView.frame = CGRectMake(20, 0, KWIDTH-40, 116);
    
    UILabel *dateConLab = [[UILabel alloc] init];
    //dateConLab.backgroundColor = [UIColor cyanColor];
    dateConLab.frame = CGRectMake(16, 11, KWIDTH-32, 17);
    dateConLab.text = @"2019-09-25 09:04:50";
    dateConLab.font = FontSize(12);
    dateConLab.textColor = K999999;
    [headerView addSubview:dateConLab];
    
    UILabel *nameTitLab = [[UILabel alloc] init];
    //nameTitLab.backgroundColor = [UIColor cyanColor];
    nameTitLab.frame = CGRectMake(16, dateConLab.bottom +9, 70, 20);
    nameTitLab.text = @"收货人";
    nameTitLab.font = FontSize(14);
    nameTitLab.textColor = K666666;
    [headerView addSubview:nameTitLab];
    
    UILabel *nameConLab = [[UILabel alloc] init];
    //nameConLab.backgroundColor = [UIColor orangeColor];
    nameConLab.frame = CGRectMake(nameTitLab.right, dateConLab.bottom +9, (KWIDTH -32-70-20)/2, 20);
    nameConLab.text = @"海燕";
    nameConLab.font = FontSize(14);
    nameConLab.textColor = [UIColor colorWithHexString:@"#272727"];
    [headerView addSubview:nameConLab];
    
    UIImageView *phoneImg = [[UIImageView alloc] init];
    //phoneImg.backgroundColor = [UIColor purpleColor];
    phoneImg.frame = CGRectMake(nameConLab.right, dateConLab.bottom +9, 20, 20);
    [phoneImg setImage:imgname(@"phone")];
    [headerView addSubview:phoneImg];
    
    UILabel *phoneLab = [[UILabel alloc] init];
    //phoneLab.backgroundColor = [UIColor greenColor];
    phoneLab.frame = CGRectMake(phoneImg.right, dateConLab.bottom +9, (KWIDTH -32-70-20)/2, 20);
    phoneLab.text = @"15958961147";
    phoneLab.textAlignment = NSTextAlignmentCenter;
    phoneLab.font = FontSize(14);
    phoneLab.textColor = [UIColor colorWithHexString:@"#272727"];
    [headerView addSubview:phoneLab];
    
    UILabel *addTitLab = [[UILabel alloc] init];
    //addTitLab.backgroundColor = [UIColor orangeColor];
    addTitLab.frame = CGRectMake(16, nameTitLab.bottom +9, 70, 20);
    addTitLab.text = @"收货地址";
    addTitLab.font = FontSize(14);
    addTitLab.textColor = K666666;
    [headerView addSubview:addTitLab];
    
    UILabel *addConLab = [[UILabel alloc] init];
    //addConLab.backgroundColor = [UIColor orangeColor];
    addConLab.frame = CGRectMake(addTitLab.right +23, nameConLab.bottom +5, KWIDTH-32-70-20, 40);
    addConLab.text = @"河南省郑州市管城回族区航海路大学路升龙国际城机会几单元3号341";
    addConLab.numberOfLines = 0;
    addConLab.font = FontSize(12);
    addConLab.textColor = K666666;
    [headerView addSubview:addConLab];
    
    UILabel *line1 = [[UILabel alloc] init];
    line1.backgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
    line1.frame = CGRectMake(0, 115, KWIDTH, 1);
    [headerView addSubview:line1];
    
    return headerView;
}

//分区区头 选择按钮 点击事件处理
- (void)secSelBtnAction:(UIButton *)button {
    
    NSLog(@"您点击了 分区 区头 的选择按钮");
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //定义Cell标识 每一个Cell对应一个自己的标识
    NSString *CellIdentitier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section, indexPath.row];
    
    //通过唯一标识创建Cell实例
    OrderManageTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentitier];
    //判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,w而不会再次初始化)
    if (!mycell) {
        mycell = [[OrderManageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentitier];
    }
    self.mytableView.separatorStyle = UITableViewCellEditingStyleNone;//不显示分割线
    
//    mycell.myblock = ^(NSUInteger ind, NSString * _Nonnull str) {
//        switch (ind) {
//            case 0: {
//                //输入名字
//                //model.name = str;
//                //NSLog(@"%@", str);
//
//                self.rowChose =! self.rowChose;
//                if (self.rowChose) {
//                    [mycell.rowSelBtn setImage:[UIImage imageNamed:@"selequyu"] forState:UIControlStateNormal];
//                } else {
//                    [mycell.rowSelBtn setImage:[UIImage imageNamed:@"seleQuyuyew"] forState:UIControlStateNormal];
//                }
//            }
//                break;
//            case 1: {
//                //修改数量 弹窗
//                [[UIApplication sharedApplication].keyWindow addSubview:self.bgViewsecConfirm];
//
//            }
//                break;
//            case 2: {
//                //取消原因 弹窗
//                [[UIApplication sharedApplication].keyWindow addSubview:self.bgViewsecCancle];
//
//            }
//                break;
//            default:
//                break;
//        }
//    };
    
    return mycell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

/**
 弹出框的背景图 取消原因
 */
-(void)shwoBgviewsecCancle{
    self.bgViewsecCancle = [[UIView alloc]init];
    self.bgViewsecCancle.frame = self.view.bounds;
    self.bgViewsecCancle.backgroundColor = RGBA(1, 1, 1, 0.5);
    UIView *whiteBGView = [[UIView alloc]init];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    whiteBGView.layer.masksToBounds = YES;
    whiteBGView.layer.cornerRadius = 6;
    [self.bgViewsecCancle addSubview:whiteBGView];
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.offset(240);
        make.left.offset(22);
        make.right.offset(-22);
    }];
    
    UILabel *UpLable = [[UILabel alloc]initWithFrame:CGRectMake(38, 48, KWIDTH-44-76, 21)];
    [whiteBGView addSubview:UpLable];
    UpLable.font = FontSize(16);
    UpLable.textColor = [UIColor colorWithHexString:@"#262626"];
    UpLable.textAlignment =  NSTextAlignmentLeft;
    UpLable.text = @"取消原因:";
    
    UITextField *cancelTF = [[UITextField alloc] init];
    //cancelTF.backgroundColor = [UIColor orangeColor];
    cancelTF.frame = CGRectMake(38, UpLable.bottom +15, KWIDTH-44-76, 39);
    cancelTF.placeholder = @"请选择取消原因";
    cancelTF.textColor = K333333;
    cancelTF.font = FontSize(14);
    cancelTF.layer.cornerRadius = 5;
    cancelTF.layer.borderColor = [UIColor colorWithHexString:@"#BFBFBF"].CGColor;
    cancelTF.layer.borderWidth = 1;
    [whiteBGView addSubview:cancelTF];
    
    UIButton *cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [cancelButton setTitle:@"暂不取消" forState:(UIControlStateNormal)];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#70BE68"] forState:(UIControlStateNormal)];
    [cancelButton setBackgroundColor:[UIColor whiteColor]];
    cancelButton.layer.masksToBounds = YES;
    cancelButton.layer.cornerRadius = 4;
    cancelButton.layer.borderWidth = 1;
    cancelButton.layer.borderColor = [UIColor colorWithHexString:@"#70BE68"].CGColor;
    cancelButton.titleLabel.font = FontSize(14);
    [cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.offset(28);
        make.right.equalTo(whiteBGView.mas_centerX).offset(-13);
        make.bottom.offset(-19);
    }];
    
    UIButton *confirmButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [confirmButton setTitle:@"确认取消" forState:(UIControlStateNormal)];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [confirmButton setBackgroundColor:[[UIColor colorWithHexString:@"#70BE68"] colorWithAlphaComponent:1]];
    confirmButton.layer.masksToBounds = YES;
    confirmButton.layer.cornerRadius = 4;
    confirmButton.titleLabel.font = FontSize(14);
    [confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.equalTo(whiteBGView.mas_centerX).offset(13);
        make.right.offset(-28);
        make.bottom.offset(-19);
    }];
}

//取消弹窗 暂不取消按钮 点击事件
-(void)cancelButtonAction:(UIButton *)but{
    [_bgViewsecCancle removeFromSuperview];
    NSLog(@"您点击了 暂不取消 按钮");
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//取消弹窗 确认取消按钮 点击事件
-(void)confirmButtonAction:(UIButton *)but{
    NSLog(@"您点击了 确定取消 按钮");
    [_bgViewsecCancle removeFromSuperview];
}

/**
 弹出框的背景图 取消原因
 */
-(void)shwoBgviewsecConfirm {
    self.bgViewsecConfirm = [[UIView alloc]init];
    self.bgViewsecConfirm.frame = self.view.bounds;
    self.bgViewsecConfirm.backgroundColor = RGBA(1, 1, 1, 0.5);
    UIView *whiteBGView = [[UIView alloc]init];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    whiteBGView.layer.masksToBounds = YES;
    whiteBGView.layer.cornerRadius = 6;
    [self.bgViewsecConfirm addSubview:whiteBGView];
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.offset(240);
        make.left.offset(22);
        make.right.offset(-22);
    }];
    
    UILabel *UpLable = [[UILabel alloc]initWithFrame:CGRectMake(73, 99, 48, 22)];
    [whiteBGView addSubview:UpLable];
    UpLable.font = FontSize(16);
    UpLable.textColor = [UIColor colorWithHexString:@"#262626"];
    UpLable.textAlignment =  NSTextAlignmentLeft;
    UpLable.text = @"数量:";
    
    UITextField *numberTF = [[UITextField alloc] init];
    //numberTF.backgroundColor = [UIColor orangeColor];
    numberTF.frame = CGRectMake(UpLable.right, 93, 136, 30);
    numberTF.placeholder = @"请输入数量";
    numberTF.textColor = K333333;
    numberTF.font = FontSize(14);
    numberTF.layer.cornerRadius = 5;
    numberTF.layer.borderColor = [UIColor colorWithHexString:@"#BFBFBF"].CGColor;
    numberTF.layer.borderWidth = 1;
    [whiteBGView addSubview:numberTF];
    
    UIButton *cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#70BE68"] forState:(UIControlStateNormal)];
    [cancelButton setBackgroundColor:[UIColor whiteColor]];
    cancelButton.layer.masksToBounds = YES;
    cancelButton.layer.cornerRadius = 4;
    cancelButton.layer.borderWidth = 1;
    cancelButton.layer.borderColor = [UIColor colorWithHexString:@"#70BE68"].CGColor;
    cancelButton.titleLabel.font = FontSize(14);
    [cancelButton addTarget:self action:@selector(cancelBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.offset(28);
        make.right.equalTo(whiteBGView.mas_centerX).offset(-13);
        make.bottom.offset(-19);
    }];
    
    UIButton *submitButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [submitButton setTitle:@"提交" forState:(UIControlStateNormal)];
    [submitButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [submitButton setBackgroundColor:[[UIColor colorWithHexString:@"#70BE68"] colorWithAlphaComponent:1]];
    submitButton.layer.masksToBounds = YES;
    submitButton.layer.cornerRadius = 4;
    submitButton.titleLabel.font = FontSize(14);
    [submitButton addTarget:self action:@selector(submitBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:submitButton];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.equalTo(whiteBGView.mas_centerX).offset(13);
        make.right.offset(-28);
        make.bottom.offset(-19);
    }];
}

//取消弹窗 暂不取消按钮 点击事件
-(void)cancelBtnAction:(UIButton *)but{
    [_bgViewsecConfirm removeFromSuperview];
    NSLog(@"您点击了 暂不取消 按钮");
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//取消弹窗 确认取消按钮 点击事件
-(void)submitBtnAction:(UIButton *)but{
    NSLog(@"您点击了 确定取消 按钮");
    [_bgViewsecConfirm removeFromSuperview];
    
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
        _scrollView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(KWIDTH, 100);
        
        adjustInset(_scrollView);
    }
    return _scrollView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
